package org.glut.wechat.moments.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.glut.wechat.moments.domain.MomentsNotice;
import org.glut.wechat.moments.service.MomentsNoticeService;
import org.glut.wechat.moments.utils.Result;
import org.glut.wechat.moments.vo.MomentNoticeItemVO;
import org.glut.wechat.moments.vo.UnreadCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 朋友圈消息通知控制器
 */
@RestController
@RequestMapping("/notice")
@CrossOrigin
public class MomentsNoticeController {

    @Autowired
    private MomentsNoticeService momentsNoticeService;

    /**
     * 根据通知用户ID分页查询通知列表
     *
     * @param noticeUserId 通知用户ID
     * @return 响应结果
     */
    @GetMapping("/user/{noticeUserId}")
    public Result getNoticesByNoticeUserId(@PathVariable Integer noticeUserId,
                                           @RequestParam(defaultValue = "1") Integer pageNo,
                                           @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<MomentNoticeItemVO> page = new Page(pageNo, pageSize);
        IPage<MomentNoticeItemVO> result = momentsNoticeService.getNoticesByNoticeUserId(page, noticeUserId);
        return Result.ok("查询成功", result);
    }


    /**
     * 批量标记为已读
     *
     * @param noticeUserId 通知用户ID
     * @return 响应结果
     */
    @PutMapping("/markAllAsRead")
    public Result markAllAsRead(@RequestParam Integer noticeUserId) {
        momentsNoticeService.markAllAsRead(noticeUserId);
        return Result.ok();
    }

    /**
     * 统计用户未读通知数量
     *
     * @param noticeUserId 通知用户ID
     * @return 响应结果
     */
    @GetMapping("/unreadCount/{noticeUserId}")
    public Result countUnreadNotices(@PathVariable Integer noticeUserId) {
        Integer count = momentsNoticeService.countUnreadNotices(noticeUserId);
        return Result.ok("查询成功", count);
    }

    @GetMapping("/noticeRead/{noticeUserId}")
    public Result noticeRead(@PathVariable Integer noticeUserId) {
        UnreadCountVO unreadCountVO = momentsNoticeService.noticeRead(noticeUserId);
        return Result.ok("查询成功", unreadCountVO);
    }

    /**
     * 删除通知
     *
     * @param id 通知ID
     * @return 响应结果
     */
    @DeleteMapping("/deleteNotice/{id}")
    public Result deleteNotice(@PathVariable Integer id) {
        momentsNoticeService.deleteNotice(id);
        return Result.ok("删除成功");
    }
}
